﻿using System;
using System.Collections.Generic;
using System.Text;
using E.Model;

using E.Enum;
using X.Common;

namespace E.Web.Common
{
    public class WebCommon
    {
        public static bool SetLanguageCookie(int lang)
        {
            try
            {
                Utils.WriteCookie("lan", lang.ToString());
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static void WriteUserCookie(string userid)
        {
            WriteUserCookie(userid, "", "", -1);
        }

        public static void WriteUserCookie(string userid, string username)
        {
            WriteUserCookie(userid, username, "", -1);
        }

        public static void WriteUserCookie(string userid, string username, string areaid, int ispublic)
        {
            Utils.WriteCookie("userid", userid);
            Utils.WriteCookie("username", username);
            Utils.WriteCookie("areaid", areaid);
            Utils.WriteCookie("ispublic", ispublic.ToString());
        }

        public static string SwitchDayOfWeek()
        {
            return SwitchDayOfWeek(DateTime.Now.DayOfWeek);
        }

        public static string SwitchDayOfWeek(DayOfWeek day)
        {
            string value = "";
            switch (day)
            {
                case DayOfWeek.Sunday:
                    value = "星期日";
                    break;
                case DayOfWeek.Monday:
                    value = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    value = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    value = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    value = "星期四";
                    break;
                case DayOfWeek.Friday:
                    value = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    value = "星期六";
                    break;
                default:
                    break;
            }
            return value;
        }

        public static string Convert2String(IList<int> parms)
        {
            if (parms.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (int parm in parms)
                {
                    sb.Append(parm);
                    sb.Append(",");
                }
                string rst = sb.ToString();
                rst = rst.Substring(0, rst.Length - 1);
                return rst;
            }
            else
                return "";
        }
        public static string Convert2String(IList<int> parms, string seperatorChar)
        {
            if (parms.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (int parm in parms)
                {
                    sb.Append(parm);
                    sb.Append(seperatorChar);
                }
                string rst = sb.ToString();
                rst = rst.Substring(0, rst.Length - 1);
                return rst;
            }
            else
                return "";
        }
        public static IList<int> Convert2IList(string parms, char seperatorChar)
        {
            IList<int> rst = new List<int>();
            string[] parmsArr = parms.Split(seperatorChar);
            foreach (string parm in parmsArr)
            {
                if (parm != "")
                {
                    int item = Convert.ToInt32(parm);
                    rst.Add(item);
                }
            }
            return rst;
        }
        public static string Convert2String(Dictionary<int, string> parms)
        {
            if (parms.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair<int, string> kvp in parms)
                {
                    sb.Append(kvp.Key);
                    sb.Append(",");
                }
                string rst = sb.Remove(sb.Length - 1, 1).ToString();
                //string rst = sb.ToString();
                //rst = rst.Substring(0, rst.Length - 1);
                return rst;
            }
            else
                return "";
              }
        public static string Convert2String(IList<QuestionModel> parms)
        {
            if (parms.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (QuestionModel parm in parms)
                {
                    sb.Append(parm.ID);
                    sb.Append(",");
                }
                string rst = sb.ToString();
                rst = rst.Substring(0, rst.Length - 1);
                return rst;
            }
            else
                return "";
        }
        /// <summary>
        /// 教师后台管理菜单
        /// </summary>
        /// <param name="authority"></param>
        /// <returns></returns>
        public static IList<MenuModel> GetMenu(EnumTeacherAuthority authority)
        {
            IList<MenuModel> menu = new List<MenuModel>();
            switch (authority)
            {
                case EnumTeacherAuthority.Admin:
                    menu.Add(new MenuModel() { RootTitle = "教师管理", RootLink = "#", SubTitle = new string[] { "教师信息", "新增教师" }, SubLink = new string[] { "/teacher/List.aspx", "/teacher/Edit.aspx?act=n" } });
                    menu.Add(new MenuModel() { RootTitle = "内训管理", RootLink = "#", SubTitle = new string[] { "项目管理", "课程管理", "学员进度查询", "学员成绩查询", "输入成绩管理", "课程满意度分析", "项目整体满意度", "技能组" }, SubLink = new string[] { "/internals/projects/List.aspx", "/internals/course/List.aspx", "/statistics/InternalProgress.aspx", "/internals/exam/ScoreList.aspx", "/internals/score/List.aspx", "/internals/statistics/Course.aspx", "/internals/statistics/Project.aspx", "/internals/skillgroup/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "外训管理", RootLink = "#", SubTitle = new string[] { "认证管理", "课程管理" }, SubLink = new string[] { "/publics/certificate/List.aspx", "/publics/course/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "课件管理", RootLink = "#", SubTitle = new string[] { "新增课件", "课件管理" }, SubLink = new string[] { "/courseware/Edit.aspx?act=n", "/courseware/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "公共课程", RootLink = "#", SubTitle = new string[] { "项目管理", "课程管理", "课件管理", "内训公共班级", "外训公共班级", "试卷管理", "课程满意度分析" }, SubLink = new string[] { "/internals/publics/projects/List.aspx", "/internals/publics/course/List.aspx", "/internals/publics/courseware/List.aspx", "/internals/publics/classes/List.aspx", "/internals/publics/classes_public/List.aspx", "/internals/publics/exam/List.aspx", "/internals/publics/statistics/Course.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "学员管理", RootLink = "#", SubTitle = new string[] { "学员信息", "新增学员", "批量新增学员", "内训学员成绩" }, SubLink = new string[] { "/student/List.aspx", "/student/Edit.aspx?act=n", "/student/EditPlural.aspx", "/student/Score.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "班级管理", RootLink = "#", SubTitle = new string[] { "开设外训班级", "外训班级信息", "外训报名审核", "开设内训班级", "内训班级信息", "内训报名审核" }, SubLink = new string[] { "/classes/Edit.aspx?act=n", "/classes/ClassList.aspx", "/classes/SignUpList.aspx", "/internals/classes/Edit.aspx?act=n", "/internals/classes/List.aspx", "/internals/classes/SignUpDeal.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "考试管理", RootLink = "#", SubTitle = new string[] { "题库管理", "课程试卷管理", "认证试卷管理", "学员成绩查询", "学员成绩输入", "题型管理", "试卷模板管理", "考试情况报表", "学习评估" }, SubLink = new string[] { "/exam/QuestionList.aspx", "/exam/ExamListCourse.aspx", "/exam/ExamList.aspx", "/exam/ScoreInquiry.aspx", "/exam/ScoreInput.aspx", "/internals/question/QuestionDefaultScoreSet.aspx", "/exam/ExamDefaultSet.aspx", "/report/exam/QuestionTestStaticsReport.aspx", "/report/exam/AverageScoreByClassReport.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "统计分析", RootLink = "#", SubTitle = new string[] { "学员进度查询", "课程满意度分析", "认证整体满意度", "学习进度报表", "登录情况报表" }, SubLink = new string[] { "/statistics/Progress.aspx", "/statistics/CourseAnalysis2.aspx", "/statistics/CertificateAnalysis.aspx", "/report/study/ReportStudentStudyInfo.aspx", "/report/study/ReportStudentLoginInfo.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "问卷管理", RootLink = "#", SubTitle = new string[] { "选项管理", "课程问卷管理", "项目问卷管理" }, SubLink = new string[] { "/internals/survey/List.aspx", "/internals/surveysetting/CourseList.aspx", "/internals/surveysetting/CertificateList.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "个人信息", RootLink = "#", SubTitle = new string[] { "修改密码", "个人信息", "安全信息" }, SubLink = new string[] { "/info/UpdatePwd.aspx", "/info/UpdateInfo.aspx", "/info/SafetyInfo.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "信息管理", RootLink = "#", SubTitle = new string[] { "公告管理", "意见反馈管理", "问题解答跟踪" }, SubLink = new string[] { "/sys/notice/List.aspx", "/sys/feedback/List.aspx", "/sys/qna/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "定时邮件", RootLink = "#", SubTitle = new string[] { "定时邮件提醒" }, SubLink = new string[] { "/Mail/MailConfig.aspx" } });
                    break;
                case EnumTeacherAuthority.Normal:
                    menu.Add(new MenuModel() { RootTitle = "学员管理", RootLink = "#", SubTitle = new string[] { "学员信息", "新增学员", "批量新增学员" }, SubLink = new string[] { "/student/List.aspx", "/student/Edit.aspx?act=n", "/student/EditPlural.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "班级管理", RootLink = "#", SubTitle = new string[] { "开设班级", "班级信息" }, SubLink = new string[] { "/classes/Edit.aspx?act=n", "/classes/ClassList.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "考试管理", RootLink = "#", SubTitle = new string[] { "题库管理", "课程试卷管理", "认证试卷管理", "学员成绩查询", "学员成绩输入" }, SubLink = new string[] { "/exam/QuestionList.aspx", "/exam/ExamListCourse.aspx", "/exam/ExamList.aspx", "/exam/ScoreInquiry.aspx", "/exam/ScoreInput.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "统计分析", RootLink = "#", SubTitle = new string[] { "学员进度查询", "课程满意度分析" }, SubLink = new string[] { "/statistics/Progress.aspx", "/statistics/CourseAnalysis2.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "个人信息", RootLink = "#", SubTitle = new string[] { "修改密码", "个人信息", "安全信息" }, SubLink = new string[] { "/info/UpdatePwd.aspx", "/info/UpdateInfo.aspx", "/info/SafetyInfo.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "师生互动", RootLink = "#", SubTitle = new string[] { "答疑" }, SubLink = new string[] { "/interactive/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "信息管理", RootLink = "#", SubTitle = new string[] { "公告管理" }, SubLink = new string[] { "/sys/notice/List.aspx" } });
                    break;
                case EnumTeacherAuthority.Internals:
                    menu.Add(new MenuModel() { RootTitle = "学员管理", RootLink = "#", SubTitle = new string[] { "学员信息", "新增学员", "批量新增学员", "学员成绩" }, SubLink = new string[] { "/student/List.aspx", "/student/Edit.aspx?act=n", "/student/EditPlural.aspx", "/student/Score.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "内训管理", RootLink = "#", SubTitle = new string[] { "课程管理" }, SubLink = new string[] { "/internals/course/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "课件管理", RootLink = "#", SubTitle = new string[] { "新增课件", "课件管理" }, SubLink = new string[] { "/courseware/Edit.aspx?act=n", "/courseware/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "班级管理", RootLink = "#", SubTitle = new string[] { "开设班级", "班级信息", "报名审核" }, SubLink = new string[] { "/internals/classes/Edit.aspx?act=n", "/internals/classes/List.aspx", "/internals/classes/SignUpDeal.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "考试管理", RootLink = "#", SubTitle = new string[] { "题库管理", "试卷管理", "在线阅卷" }, SubLink = new string[] { "/internals/question/List.aspx", "/internals/exam/List.aspx", "/internals/exam/PaperList.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "统计分析", RootLink = "#", SubTitle = new string[] { "学员进度查询", "学员成绩查询", "课程满意度分析" }, SubLink = new string[] { "/statistics/InternalProgress.aspx", "/internals/exam/ScoreList.aspx", "/internals/statistics/Course.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "个人信息", RootLink = "#", SubTitle = new string[] { "修改密码", "个人信息", "安全信息" }, SubLink = new string[] { "/info/UpdatePwd.aspx", "/info/UpdateInfo.aspx", "/info/SafetyInfo.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "师生互动", RootLink = "#", SubTitle = new string[] { "答疑" }, SubLink = new string[] { "/interactive/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "信息管理", RootLink = "#", SubTitle = new string[] { "公告管理" }, SubLink = new string[] { "/sys/notice/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "定时邮件", RootLink = "#", SubTitle = new string[] { "定时邮件提醒" }, SubLink = new string[] { "/Mail/MailConfig.aspx" } });
                    break;
                default: break;
            }
            return menu;
        }

        /// <summary>
        /// 用户后台菜单
        /// </summary>
        /// <param name="authority"></param>
        /// <returns></returns>
        public static IList<MenuModel> GetUserMenu(EnumUserAuthority authority)
        {
            IList<MenuModel> menu = new List<MenuModel>();
            switch (authority)
            {
                case EnumUserAuthority.Public:
                    menu.Add(new MenuModel() { RootTitle = "学习报名", RootLink = "#", SubTitle = new string[] { "课程选择" }, SubLink = new string[] { "/training/signup/Register.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "我的课程", RootLink = "#", SubTitle = new string[] { "课程列表", "我的学习历程" }, SubLink = new string[] { "/training/course/CourseList.aspx", "/training/course/LearningProcess.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "我的考试", RootLink = "#", SubTitle = new string[] { "参加课程考试", "参加认证考试" }, SubLink = new string[] { "/training/exam/LessonExam.aspx", "/training/exam/CertificateExam.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "我的成绩", RootLink = "#", SubTitle = new string[] { "课程成绩统计", "我的认证" }, SubLink = new string[] { "/training/score/ScoreStatistics.aspx", "/training/score/MyCertificate.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "师生互动", RootLink = "#", SubTitle = new string[] { "答疑" }, SubLink = new string[] { "/training/interactive/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "个人资料", RootLink = "#", SubTitle = new string[] { "修改密码", "个人信息", "安全信息" }, SubLink = new string[] { "/training/info/UpdatePwd.aspx", "/training/info/UpdateInfo.aspx", "/training/info/SafetyInfo.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "满意度调查", RootLink = "#", SubTitle = new string[] { "满意度调查" }, SubLink = new string[] { "/training/survey/List.aspx" } });
                    break;
                case EnumUserAuthority.Internal:
                    menu.Add(new MenuModel() { RootTitle = "学习报名", RootLink = "#", SubTitle = new string[] { "课程选择", "自选课程" }, SubLink = new string[] { "/training/internals/classes/List.aspx","/training/internals/course/FreeCourseList.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "我的课程", RootLink = "#", SubTitle = new string[] { "课程列表", "我的学习历程" }, SubLink = new string[] { "/training/internals/course/List.aspx", "/training/internals/course/Process.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "公共课程", RootLink = "#", SubTitle = new string[] { "外训公共班级", "内训公共班级", "外训公共课程", "内训公共课程", "公共课程考试" }, SubLink = new string[] { "/training/internals/publics/classes/PublicClassList.aspx", "/training/internals/publics/classes/InternalPublicClassList.aspx", "/training/internals/publics/course/PublicCourseList.aspx", "/training/internals/publics/course/InternalPublicCourseList.aspx", "/training/internals/publics/exam/List_Course.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "我的考试", RootLink = "#", SubTitle = new string[] { "课程考试", "补考" }, SubLink = new string[] { "/training/internals/exam/course/List.aspx", "/training/internals/exam/course/SecList.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "我的成绩", RootLink = "#", SubTitle = new string[] { "课程成绩统计", "输入成绩" }, SubLink = new string[] { "/training/internals/statistics/Info.aspx", "/training/internals/statistics/Score.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "满意度调查", RootLink = "#", SubTitle = new string[] { "课程满意度", "项目满意度" }, SubLink = new string[] { "/training/internals/survey/CourseSurveyList.aspx", "/training/internals/survey/CertificateSurveyList.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "师生互动", RootLink = "#", SubTitle = new string[] { "答疑" }, SubLink = new string[] { "/training/interactive/List.aspx" } });
                    menu.Add(new MenuModel() { RootTitle = "个人资料", RootLink = "#", SubTitle = new string[] { "修改密码", "个人信息", "安全信息" }, SubLink = new string[] { "/training/info/UpdatePwd.aspx", "/training/info/UpdateInfo.aspx", "/training/info/SafetyInfo.aspx" } });
                    break;
                default: break;
            }
            return menu;
        }

        /// <summary>
        /// 向页面输出xml内容
        /// </summary>
        /// <param name="xmlnode">xml内容</param>
        public static void ResponseXML(System.Text.StringBuilder xmlnode)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/XML";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Cache.SetNoStore();
            System.Web.HttpContext.Current.Response.Write(xmlnode.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

        public static void ExportExcelData(System.Text.StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            //System.Web.HttpContext.Current.Response.Charset = "GB2312";
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Analysis" + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            //System.Web.HttpContext.Current.e = false;
            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}
